<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.test.utils &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django.test" href="../test.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-test-utils">
            
  <h1>Source code for django.test.utils</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">contextmanager</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">unittest</span> <span class="kn">import</span> <span class="n">skipUnless</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span>
<span class="kn">from</span> <span class="nn">xml.dom.minidom</span> <span class="kn">import</span> <span class="n">parseString</span><span class="p">,</span> <span class="n">Node</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="kn">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span><span class="p">,</span> <span class="n">UserSettingsHolder</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">mail</span>
<span class="kn">from</span> <span class="nn">django.core.signals</span> <span class="kn">import</span> <span class="n">request_started</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">reset_queries</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">request</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="kn">import</span> <span class="n">Template</span><span class="p">,</span> <span class="n">loader</span><span class="p">,</span> <span class="n">TemplateDoesNotExist</span>
<span class="kn">from</span> <span class="nn">django.template.loaders</span> <span class="kn">import</span> <span class="n">cached</span>
<span class="kn">from</span> <span class="nn">django.test.signals</span> <span class="kn">import</span> <span class="n">template_rendered</span><span class="p">,</span> <span class="n">setting_changed</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">RemovedInDjango19Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_str</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">deactivate</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s">&#39;Approximate&#39;</span><span class="p">,</span> <span class="s">&#39;ContextList&#39;</span><span class="p">,</span> <span class="s">&#39;get_runner&#39;</span><span class="p">,</span>
    <span class="s">&#39;modify_settings&#39;</span><span class="p">,</span> <span class="s">&#39;override_settings&#39;</span><span class="p">,</span>
    <span class="s">&#39;requires_tz_support&#39;</span><span class="p">,</span>
    <span class="s">&#39;setup_test_environment&#39;</span><span class="p">,</span> <span class="s">&#39;teardown_test_environment&#39;</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">RESTORE_LOADERS_ATTR</span> <span class="o">=</span> <span class="s">&#39;_original_template_source_loaders&#39;</span>
<span class="n">TZ_SUPPORT</span> <span class="o">=</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="s">&#39;tzset&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Approximate</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">places</span><span class="o">=</span><span class="mi">7</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">places</span> <span class="o">=</span> <span class="n">places</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">==</span> <span class="n">other</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">-</span> <span class="n">other</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">places</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>


<span class="k">class</span> <span class="nc">ContextList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A wrapper that provides direct key access to context items contained</span>
<span class="sd">    in a list of context objects.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">subcontext</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">subcontext</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">subcontext</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ContextList</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">return</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Flattened keys of subcontexts.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">subcontext</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>
            <span class="k">for</span> <span class="nb">dict</span> <span class="ow">in</span> <span class="n">subcontext</span><span class="p">:</span>
                <span class="n">keys</span> <span class="o">|=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">keys</span>


<span class="k">def</span> <span class="nf">instrumented_test_render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An instrumented Template render method, providing a signal</span>
<span class="sd">    that can be intercepted by the test system Client</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">template_rendered</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodelist</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>


<div class="viewcode-block" id="setup_test_environment"><a class="viewcode-back" href="../../../topics/testing/advanced.html#django.test.utils.setup_test_environment">[docs]</a><span class="k">def</span> <span class="nf">setup_test_environment</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Perform any global pre-test setup. This involves:</span>

<span class="sd">        - Installing the instrumented test renderer</span>
<span class="sd">        - Set the email backend to the locmem email backend.</span>
<span class="sd">        - Setting the active locale to match the LANGUAGE_CODE setting.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">Template</span><span class="o">.</span><span class="n">_original_render</span> <span class="o">=</span> <span class="n">Template</span><span class="o">.</span><span class="n">_render</span>
    <span class="n">Template</span><span class="o">.</span><span class="n">_render</span> <span class="o">=</span> <span class="n">instrumented_test_render</span>

    <span class="c"># Storing previous values in the settings module itself is problematic.</span>
    <span class="c"># Store them in arbitrary (but related) modules instead. See #20636.</span>

    <span class="n">mail</span><span class="o">.</span><span class="n">_original_email_backend</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_BACKEND</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_BACKEND</span> <span class="o">=</span> <span class="s">&#39;django.core.mail.backends.locmem.EmailBackend&#39;</span>

    <span class="n">request</span><span class="o">.</span><span class="n">_original_allowed_hosts</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">ALLOWED_HOSTS</span>
    <span class="n">settings</span><span class="o">.</span><span class="n">ALLOWED_HOSTS</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;*&#39;</span><span class="p">]</span>

    <span class="n">mail</span><span class="o">.</span><span class="n">outbox</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="n">deactivate</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="teardown_test_environment"><a class="viewcode-back" href="../../../topics/testing/advanced.html#django.test.utils.teardown_test_environment">[docs]</a><span class="k">def</span> <span class="nf">teardown_test_environment</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Perform any global post-test teardown. This involves:</span>

<span class="sd">        - Restoring the original test renderer</span>
<span class="sd">        - Restoring the email sending functions</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">Template</span><span class="o">.</span><span class="n">_render</span> <span class="o">=</span> <span class="n">Template</span><span class="o">.</span><span class="n">_original_render</span>
    <span class="k">del</span> <span class="n">Template</span><span class="o">.</span><span class="n">_original_render</span>

    <span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_BACKEND</span> <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">_original_email_backend</span>
    <span class="k">del</span> <span class="n">mail</span><span class="o">.</span><span class="n">_original_email_backend</span>

    <span class="n">settings</span><span class="o">.</span><span class="n">ALLOWED_HOSTS</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">_original_allowed_hosts</span>
    <span class="k">del</span> <span class="n">request</span><span class="o">.</span><span class="n">_original_allowed_hosts</span>

    <span class="k">del</span> <span class="n">mail</span><span class="o">.</span><span class="n">outbox</span>

</div>
<span class="k">def</span> <span class="nf">get_runner</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">test_runner_class</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">test_runner_class</span><span class="p">:</span>
        <span class="n">test_runner_class</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">TEST_RUNNER</span>

    <span class="n">test_path</span> <span class="o">=</span> <span class="n">test_runner_class</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
    <span class="c"># Allow for Python 2.5 relative paths</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">test_path</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">test_module_name</span> <span class="o">=</span> <span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">test_path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">test_module_name</span> <span class="o">=</span> <span class="s">&#39;.&#39;</span>
    <span class="n">test_module</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="n">test_module_name</span><span class="p">,</span> <span class="p">{},</span> <span class="p">{},</span> <span class="n">force_str</span><span class="p">(</span><span class="n">test_path</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
    <span class="n">test_runner</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">test_module</span><span class="p">,</span> <span class="n">test_path</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">test_runner</span>


<span class="k">def</span> <span class="nf">setup_test_template_loader</span><span class="p">(</span><span class="n">templates_dict</span><span class="p">,</span> <span class="n">use_cached_loader</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Changes Django to only find templates from within a dictionary (where each</span>
<span class="sd">    key is the template name and each value is the corresponding template</span>
<span class="sd">    content to return).</span>

<span class="sd">    Use meth:`restore_template_loaders` to restore the original loaders.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">RESTORE_LOADERS_ATTR</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;loader.</span><span class="si">%s</span><span class="s"> already exists&quot;</span> <span class="o">%</span> <span class="n">RESTORE_LOADERS_ATTR</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">test_template_loader</span><span class="p">(</span><span class="n">template_name</span><span class="p">,</span> <span class="n">template_dirs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="s">&quot;A custom template loader that loads templates from a dictionary.&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">templates_dict</span><span class="p">[</span><span class="n">template_name</span><span class="p">],</span> <span class="s">&quot;test:</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">template_name</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">TemplateDoesNotExist</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">use_cached_loader</span><span class="p">:</span>
        <span class="n">template_loader</span> <span class="o">=</span> <span class="n">cached</span><span class="o">.</span><span class="n">Loader</span><span class="p">((</span><span class="s">&#39;test_template_loader&#39;</span><span class="p">,))</span>
        <span class="n">template_loader</span><span class="o">.</span><span class="n">_cached_loaders</span> <span class="o">=</span> <span class="p">(</span><span class="n">test_template_loader</span><span class="p">,)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">template_loader</span> <span class="o">=</span> <span class="n">test_template_loader</span>

    <span class="nb">setattr</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">RESTORE_LOADERS_ATTR</span><span class="p">,</span> <span class="n">loader</span><span class="o">.</span><span class="n">template_source_loaders</span><span class="p">)</span>
    <span class="n">loader</span><span class="o">.</span><span class="n">template_source_loaders</span> <span class="o">=</span> <span class="p">(</span><span class="n">template_loader</span><span class="p">,)</span>
    <span class="k">return</span> <span class="n">template_loader</span>


<span class="k">def</span> <span class="nf">restore_template_loaders</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Restores the original template loaders after</span>
<span class="sd">    :meth:`setup_test_template_loader` has been run.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">loader</span><span class="o">.</span><span class="n">template_source_loaders</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">RESTORE_LOADERS_ATTR</span><span class="p">)</span>
    <span class="nb">delattr</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">RESTORE_LOADERS_ATTR</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">override_settings</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Acts as either a decorator, or a context manager. If it&#39;s a decorator it</span>
<span class="sd">    takes a function and returns a wrapped function. If it&#39;s a contextmanager</span>
<span class="sd">    it&#39;s used with the ``with`` statement. In either event entering/exiting</span>
<span class="sd">    are called before and after, respectively, the function/block is executed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disable</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test_func</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.test</span> <span class="kn">import</span> <span class="n">SimpleTestCase</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">test_func</span><span class="p">,</span> <span class="nb">type</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">test_func</span><span class="p">,</span> <span class="n">SimpleTestCase</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span>
                    <span class="s">&quot;Only subclasses of Django SimpleTestCase can be decorated &quot;</span>
                    <span class="s">&quot;with override_settings&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">save_options</span><span class="p">(</span><span class="n">test_func</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">test_func</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nd">@wraps</span><span class="p">(</span><span class="n">test_func</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
                <span class="k">with</span> <span class="bp">self</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">test_func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inner</span>

    <span class="k">def</span> <span class="nf">save_options</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test_func</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">test_func</span><span class="o">.</span><span class="n">_overridden_settings</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">test_func</span><span class="o">.</span><span class="n">_overridden_settings</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Duplicate dict to prevent subclasses from altering their parent.</span>
            <span class="n">test_func</span><span class="o">.</span><span class="n">_overridden_settings</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
                <span class="n">test_func</span><span class="o">.</span><span class="n">_overridden_settings</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">enable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Keep this code at the beginning to leave the settings unchanged</span>
        <span class="c"># in case it raises an exception because INSTALLED_APPS is invalid.</span>
        <span class="k">if</span> <span class="s">&#39;INSTALLED_APPS&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">apps</span><span class="o">.</span><span class="n">set_installed_apps</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s">&#39;INSTALLED_APPS&#39;</span><span class="p">])</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">apps</span><span class="o">.</span><span class="n">unset_installed_apps</span><span class="p">()</span>
                <span class="k">raise</span>
        <span class="n">override</span> <span class="o">=</span> <span class="n">UserSettingsHolder</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">new_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">override</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">new_value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapped</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span>
        <span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span> <span class="o">=</span> <span class="n">override</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">new_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">setting_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span>
                                 <span class="n">setting</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">new_value</span><span class="p">,</span> <span class="n">enter</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">disable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="s">&#39;INSTALLED_APPS&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
            <span class="n">apps</span><span class="o">.</span><span class="n">unset_installed_apps</span><span class="p">()</span>
        <span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapped</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapped</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
            <span class="n">new_value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="n">setting_changed</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span>
                                 <span class="n">setting</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">new_value</span><span class="p">,</span> <span class="n">enter</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">modify_settings</span><span class="p">(</span><span class="n">override_settings</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Like override_settings, but makes it possible to append, prepend or remove</span>
<span class="sd">    items instead of redefining the entire list.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">args</span><span class="p">:</span>
            <span class="c"># Hack used when instantiating from SimpleTestCase._pre_setup.</span>
            <span class="k">assert</span> <span class="ow">not</span> <span class="n">kwargs</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">operations</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="ow">not</span> <span class="n">args</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">operations</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">save_options</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test_func</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">test_func</span><span class="o">.</span><span class="n">_modified_settings</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">test_func</span><span class="o">.</span><span class="n">_modified_settings</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">operations</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Duplicate list to prevent subclasses from altering their parent.</span>
            <span class="n">test_func</span><span class="o">.</span><span class="n">_modified_settings</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span>
                <span class="n">test_func</span><span class="o">.</span><span class="n">_modified_settings</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">operations</span>

    <span class="k">def</span> <span class="nf">enable</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">operations</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">operations</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="c"># When called from SimpleTestCase._pre_setup, values may be</span>
                <span class="c"># overridden several times; cumulate changes.</span>
                <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="p">[]))</span>
            <span class="k">for</span> <span class="n">action</span><span class="p">,</span> <span class="n">items</span> <span class="ow">in</span> <span class="n">operations</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="c"># items my be a single value or an iterable.</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">items</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                    <span class="n">items</span> <span class="o">=</span> <span class="p">[</span><span class="n">items</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s">&#39;append&#39;</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="n">value</span> <span class="o">+</span> <span class="p">[</span><span class="n">item</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">items</span> <span class="k">if</span> <span class="n">item</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">]</span>
                <span class="k">elif</span> <span class="n">action</span> <span class="o">==</span> <span class="s">&#39;prepend&#39;</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">item</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">items</span> <span class="k">if</span> <span class="n">item</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">]</span> <span class="o">+</span> <span class="n">value</span>
                <span class="k">elif</span> <span class="n">action</span> <span class="o">==</span> <span class="s">&#39;remove&#39;</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">item</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">value</span> <span class="k">if</span> <span class="n">item</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">items</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Unsupported action: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">action</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">modify_settings</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">enable</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">override_system_checks</span><span class="p">(</span><span class="n">new_checks</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Acts as a decorator. Overrides list of registered system checks.</span>
<span class="sd">    Useful when you override `INSTALLED_APPS`, e.g. if you exclude `auth` app,</span>
<span class="sd">    you also need to exclude its system checks. &quot;&quot;&quot;</span>

    <span class="kn">from</span> <span class="nn">django.core.checks.registry</span> <span class="kn">import</span> <span class="n">registry</span>

    <span class="k">def</span> <span class="nf">outer</span><span class="p">(</span><span class="n">test_func</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">test_func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="n">old_checks</span> <span class="o">=</span> <span class="n">registry</span><span class="o">.</span><span class="n">registered_checks</span>
            <span class="n">registry</span><span class="o">.</span><span class="n">registered_checks</span> <span class="o">=</span> <span class="n">new_checks</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">test_func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="n">registry</span><span class="o">.</span><span class="n">registered_checks</span> <span class="o">=</span> <span class="n">old_checks</span>
        <span class="k">return</span> <span class="n">inner</span>
    <span class="k">return</span> <span class="n">outer</span>


<span class="k">def</span> <span class="nf">compare_xml</span><span class="p">(</span><span class="n">want</span><span class="p">,</span> <span class="n">got</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Tries to do a &#39;xml-comparison&#39; of want and got.  Plain string</span>
<span class="sd">    comparison doesn&#39;t always work because, for example, attribute</span>
<span class="sd">    ordering should not be important. Comment nodes are not considered in the</span>
<span class="sd">    comparison.</span>

<span class="sd">    Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_norm_whitespace_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;[ \t\n][ \t\n]+&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">norm_whitespace</span><span class="p">(</span><span class="n">v</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_norm_whitespace_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">child_text</span><span class="p">(</span><span class="n">element</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">c</span><span class="o">.</span><span class="n">data</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">element</span><span class="o">.</span><span class="n">childNodes</span>
                        <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">nodeType</span> <span class="o">==</span> <span class="n">Node</span><span class="o">.</span><span class="n">TEXT_NODE</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">children</span><span class="p">(</span><span class="n">element</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">element</span><span class="o">.</span><span class="n">childNodes</span>
                <span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">nodeType</span> <span class="o">==</span> <span class="n">Node</span><span class="o">.</span><span class="n">ELEMENT_NODE</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">norm_child_text</span><span class="p">(</span><span class="n">element</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">norm_whitespace</span><span class="p">(</span><span class="n">child_text</span><span class="p">(</span><span class="n">element</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">attrs_dict</span><span class="p">(</span><span class="n">element</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="n">element</span><span class="o">.</span><span class="n">attributes</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">check_element</span><span class="p">(</span><span class="n">want_element</span><span class="p">,</span> <span class="n">got_element</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">want_element</span><span class="o">.</span><span class="n">tagName</span> <span class="o">!=</span> <span class="n">got_element</span><span class="o">.</span><span class="n">tagName</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="n">norm_child_text</span><span class="p">(</span><span class="n">want_element</span><span class="p">)</span> <span class="o">!=</span> <span class="n">norm_child_text</span><span class="p">(</span><span class="n">got_element</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="n">attrs_dict</span><span class="p">(</span><span class="n">want_element</span><span class="p">)</span> <span class="o">!=</span> <span class="n">attrs_dict</span><span class="p">(</span><span class="n">got_element</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="n">want_children</span> <span class="o">=</span> <span class="n">children</span><span class="p">(</span><span class="n">want_element</span><span class="p">)</span>
        <span class="n">got_children</span> <span class="o">=</span> <span class="n">children</span><span class="p">(</span><span class="n">got_element</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">want_children</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">got_children</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">for</span> <span class="n">want</span><span class="p">,</span> <span class="n">got</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">want_children</span><span class="p">,</span> <span class="n">got_children</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">check_element</span><span class="p">(</span><span class="n">want</span><span class="p">,</span> <span class="n">got</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">False</span>
        <span class="k">return</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">first_node</span><span class="p">(</span><span class="n">document</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">document</span><span class="o">.</span><span class="n">childNodes</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">nodeType</span> <span class="o">!=</span> <span class="n">Node</span><span class="o">.</span><span class="n">COMMENT_NODE</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">node</span>

    <span class="n">want</span><span class="p">,</span> <span class="n">got</span> <span class="o">=</span> <span class="n">strip_quotes</span><span class="p">(</span><span class="n">want</span><span class="p">,</span> <span class="n">got</span><span class="p">)</span>
    <span class="n">want</span> <span class="o">=</span> <span class="n">want</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">n&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
    <span class="n">got</span> <span class="o">=</span> <span class="n">got</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">n&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>

    <span class="c"># If the string is not a complete xml document, we may need to add a</span>
    <span class="c"># root element. This allow us to compare fragments, like &quot;&lt;foo/&gt;&lt;bar/&gt;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">want</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;&lt;?xml&#39;</span><span class="p">):</span>
        <span class="n">wrapper</span> <span class="o">=</span> <span class="s">&#39;&lt;root&gt;</span><span class="si">%s</span><span class="s">&lt;/root&gt;&#39;</span>
        <span class="n">want</span> <span class="o">=</span> <span class="n">wrapper</span> <span class="o">%</span> <span class="n">want</span>
        <span class="n">got</span> <span class="o">=</span> <span class="n">wrapper</span> <span class="o">%</span> <span class="n">got</span>

    <span class="c"># Parse the want and got strings, and compare the parsings.</span>
    <span class="n">want_root</span> <span class="o">=</span> <span class="n">first_node</span><span class="p">(</span><span class="n">parseString</span><span class="p">(</span><span class="n">want</span><span class="p">))</span>
    <span class="n">got_root</span> <span class="o">=</span> <span class="n">first_node</span><span class="p">(</span><span class="n">parseString</span><span class="p">(</span><span class="n">got</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">check_element</span><span class="p">(</span><span class="n">want_root</span><span class="p">,</span> <span class="n">got_root</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">strip_quotes</span><span class="p">(</span><span class="n">want</span><span class="p">,</span> <span class="n">got</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Strip quotes of doctests output values:</span>

<span class="sd">    &gt;&gt;&gt; strip_quotes(&quot;&#39;foo&#39;&quot;)</span>
<span class="sd">    &quot;foo&quot;</span>
<span class="sd">    &gt;&gt;&gt; strip_quotes(&#39;&quot;foo&quot;&#39;)</span>
<span class="sd">    &quot;foo&quot;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">is_quoted_string</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">2</span>
                <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&quot;&#39;&quot;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">is_quoted_unicode</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">3</span>
                <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;u&#39;</span>
                <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&quot;&#39;&quot;</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">is_quoted_string</span><span class="p">(</span><span class="n">want</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_quoted_string</span><span class="p">(</span><span class="n">got</span><span class="p">):</span>
        <span class="n">want</span> <span class="o">=</span> <span class="n">want</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">got</span> <span class="o">=</span> <span class="n">got</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">elif</span> <span class="n">is_quoted_unicode</span><span class="p">(</span><span class="n">want</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_quoted_unicode</span><span class="p">(</span><span class="n">got</span><span class="p">):</span>
        <span class="n">want</span> <span class="o">=</span> <span class="n">want</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">got</span> <span class="o">=</span> <span class="n">got</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">want</span><span class="p">,</span> <span class="n">got</span>


<span class="k">def</span> <span class="nf">str_prefix</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">s</span> <span class="o">%</span> <span class="p">{</span><span class="s">&#39;_&#39;</span><span class="p">:</span> <span class="s">&#39;&#39;</span> <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span> <span class="k">else</span> <span class="s">&#39;u&#39;</span><span class="p">}</span>


<span class="k">class</span> <span class="nc">CaptureQueriesContext</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Context manager that captures queries executed by the specified connection.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">captured_queries</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">captured_queries</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">captured_queries</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">captured_queries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">initial_queries</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">final_queries</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_debug_cursor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">use_debug_cursor</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">use_debug_cursor</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">initial_queries</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">final_queries</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="n">request_started</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">reset_queries</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">use_debug_cursor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_debug_cursor</span>
        <span class="n">request_started</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">reset_queries</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">final_queries</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">IgnoreDeprecationWarningsMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">warning_classes</span> <span class="o">=</span> <span class="p">[</span><span class="n">RemovedInDjango18Warning</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">IgnoreDeprecationWarningsMixin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">setUp</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">catch_warnings</span> <span class="o">=</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">catch_warnings</span><span class="o">.</span><span class="n">__enter__</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">warning_class</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">warning_classes</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="s">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">category</span><span class="o">=</span><span class="n">warning_class</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">catch_warnings</span><span class="o">.</span><span class="n">__exit__</span><span class="p">(</span><span class="o">*</span><span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">())</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">IgnoreDeprecationWarningsMixin</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">tearDown</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">IgnorePendingDeprecationWarningsMixin</span><span class="p">(</span><span class="n">IgnoreDeprecationWarningsMixin</span><span class="p">):</span>
        <span class="n">warning_classes</span> <span class="o">=</span> <span class="p">[</span><span class="n">RemovedInDjango19Warning</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">IgnoreAllDeprecationWarningsMixin</span><span class="p">(</span><span class="n">IgnoreDeprecationWarningsMixin</span><span class="p">):</span>
        <span class="n">warning_classes</span> <span class="o">=</span> <span class="p">[</span><span class="n">RemovedInDjango19Warning</span><span class="p">,</span> <span class="n">RemovedInDjango18Warning</span><span class="p">]</span>


<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">patch_logger</span><span class="p">(</span><span class="n">logger_name</span><span class="p">,</span> <span class="n">log_level</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Context manager that takes a named logger and the logging level</span>
<span class="sd">    and provides a simple mock-like list of messages received</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">calls</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">replacement</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">calls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span> <span class="o">%</span> <span class="n">args</span><span class="p">)</span>
    <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">logger_name</span><span class="p">)</span>
    <span class="n">orig</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">logger</span><span class="p">,</span> <span class="n">log_level</span><span class="p">)</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">logger</span><span class="p">,</span> <span class="n">log_level</span><span class="p">,</span> <span class="n">replacement</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">calls</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">logger</span><span class="p">,</span> <span class="n">log_level</span><span class="p">,</span> <span class="n">orig</span><span class="p">)</span>


<span class="c"># On OSes that don&#39;t provide tzset (Windows), we can&#39;t set the timezone</span>
<span class="c"># in which the program runs. As a consequence, we must skip tests that</span>
<span class="c"># don&#39;t enforce a specific timezone (with timezone.override or equivalent),</span>
<span class="c"># or attempt to interpret naive datetimes in the default timezone.</span>

<span class="n">requires_tz_support</span> <span class="o">=</span> <span class="n">skipUnless</span><span class="p">(</span><span class="n">TZ_SUPPORT</span><span class="p">,</span>
        <span class="s">&quot;This test relies on the ability to run a program in an arbitrary &quot;</span>
        <span class="s">&quot;time zone, but your operating system isn&#39;t able to do that.&quot;</span><span class="p">)</span>


<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">extend_sys_path</span><span class="p">(</span><span class="o">*</span><span class="n">paths</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Context manager to temporarily add paths to sys.path.&quot;&quot;&quot;</span>
    <span class="n">_orig_sys_path</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">[:]</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">yield</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">_orig_sys_path</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
          <ul><li><a href="../test.html">django.test</a>
        
        <ul><li>django.test.utils</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>